using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CDTM.Modules.TraCuuDiem.Components
{
	public partial class TotNghiepTC
	{
		#region Properties.
		
		public string MaSV { set; get; }
		public string HoTenLot { set; get; }
		public string Ten { set; get; }
		public string Lop { set; get; }
		public double DiemTBCTK { set; get; }
		public DateTime KyLuat { set; get; }
		public double MontTN_ChinhTri { set; get; }
		public double MonTN_LyThuyet { set; get; }
		public double MonTN_ThucHanh { set; get; }
		public double DTB_TotNghiep { set; get; }
		public double DXL_TotNghiep { set; get; }
		public double TyLeDVHPTL { set; get; }
		public string CongNhanTN { set; get; }
		public string XepLoaiTN { set; get; }
		public string GhiChu { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static TotNghiepTC Load(string maSV)
		{
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, maSV);
			TotNghiepTC entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new TotNghiepTC();
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
				if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTBCTK"))) entity.DiemTBCTK = reader.GetDouble(reader.GetOrdinal("DiemTBCTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("KyLuat"))) entity.KyLuat = reader.GetDateTime(reader.GetOrdinal("KyLuat"));
				if (!reader.IsDBNull(reader.GetOrdinal("MontTN_ChinhTri"))) entity.MontTN_ChinhTri = reader.GetDouble(reader.GetOrdinal("MontTN_ChinhTri"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTN_LyThuyet"))) entity.MonTN_LyThuyet = reader.GetDouble(reader.GetOrdinal("MonTN_LyThuyet"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTN_ThucHanh"))) entity.MonTN_ThucHanh = reader.GetDouble(reader.GetOrdinal("MonTN_ThucHanh"));
				if (!reader.IsDBNull(reader.GetOrdinal("DTB_TotNghiep"))) entity.DTB_TotNghiep = reader.GetDouble(reader.GetOrdinal("DTB_TotNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("DXL_TotNghiep"))) entity.DXL_TotNghiep = reader.GetDouble(reader.GetOrdinal("DXL_TotNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("TyLeDVHPTL"))) entity.TyLeDVHPTL = reader.GetDouble(reader.GetOrdinal("TyLeDVHPTL"));
				if (!reader.IsDBNull(reader.GetOrdinal("CongNhanTN"))) entity.CongNhanTN = reader.GetString(reader.GetOrdinal("CongNhanTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoaiTN"))) entity.XepLoaiTN = reader.GetString(reader.GetOrdinal("XepLoaiTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<TotNghiepTC> SelectCollectionAll()
		{
			List<TotNghiepTC> collection = new List<TotNghiepTC>();
			SqlDataReader reader = (SqlDataReader) SelectReaderAll();
			while (reader.Read())
			{
				TotNghiepTC entity = new TotNghiepTC();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
				if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTBCTK"))) entity.DiemTBCTK = reader.GetDouble(reader.GetOrdinal("DiemTBCTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("KyLuat"))) entity.KyLuat = reader.GetDateTime(reader.GetOrdinal("KyLuat"));
				if (!reader.IsDBNull(reader.GetOrdinal("MontTN_ChinhTri"))) entity.MontTN_ChinhTri = reader.GetDouble(reader.GetOrdinal("MontTN_ChinhTri"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTN_LyThuyet"))) entity.MonTN_LyThuyet = reader.GetDouble(reader.GetOrdinal("MonTN_LyThuyet"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTN_ThucHanh"))) entity.MonTN_ThucHanh = reader.GetDouble(reader.GetOrdinal("MonTN_ThucHanh"));
				if (!reader.IsDBNull(reader.GetOrdinal("DTB_TotNghiep"))) entity.DTB_TotNghiep = reader.GetDouble(reader.GetOrdinal("DTB_TotNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("DXL_TotNghiep"))) entity.DXL_TotNghiep = reader.GetDouble(reader.GetOrdinal("DXL_TotNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("TyLeDVHPTL"))) entity.TyLeDVHPTL = reader.GetDouble(reader.GetOrdinal("TyLeDVHPTL"));
				if (!reader.IsDBNull(reader.GetOrdinal("CongNhanTN"))) entity.CongNhanTN = reader.GetString(reader.GetOrdinal("CongNhanTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoaiTN"))) entity.XepLoaiTN = reader.GetString(reader.GetOrdinal("XepLoaiTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<TotNghiepTC> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			List<TotNghiepTC> collection = new List<TotNghiepTC>();

			SqlDataReader reader = (SqlDataReader) SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				TotNghiepTC entity = new TotNghiepTC();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
				if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTBCTK"))) entity.DiemTBCTK = reader.GetDouble(reader.GetOrdinal("DiemTBCTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("KyLuat"))) entity.KyLuat = reader.GetDateTime(reader.GetOrdinal("KyLuat"));
				if (!reader.IsDBNull(reader.GetOrdinal("MontTN_ChinhTri"))) entity.MontTN_ChinhTri = reader.GetDouble(reader.GetOrdinal("MontTN_ChinhTri"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTN_LyThuyet"))) entity.MonTN_LyThuyet = reader.GetDouble(reader.GetOrdinal("MonTN_LyThuyet"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTN_ThucHanh"))) entity.MonTN_ThucHanh = reader.GetDouble(reader.GetOrdinal("MonTN_ThucHanh"));
				if (!reader.IsDBNull(reader.GetOrdinal("DTB_TotNghiep"))) entity.DTB_TotNghiep = reader.GetDouble(reader.GetOrdinal("DTB_TotNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("DXL_TotNghiep"))) entity.DXL_TotNghiep = reader.GetDouble(reader.GetOrdinal("DXL_TotNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("TyLeDVHPTL"))) entity.TyLeDVHPTL = reader.GetDouble(reader.GetOrdinal("TyLeDVHPTL"));
				if (!reader.IsDBNull(reader.GetOrdinal("CongNhanTN"))) entity.CongNhanTN = reader.GetString(reader.GetOrdinal("CongNhanTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoaiTN"))) entity.XepLoaiTN = reader.GetString(reader.GetOrdinal("XepLoaiTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertTotNghiepTC(string hoTenLot, string ten, string lop, double diemTBCTK, DateTime kyLuat, double montTN_ChinhTri, double monTN_LyThuyet, double monTN_ThucHanh, double dTB_TotNghiep, double dXL_TotNghiep, double tyLeDVHPTL, string congNhanTN, string xepLoaiTN, string ghiChu)
		{
			TotNghiepTC entity = new TotNghiepTC();	
			entity.HoTenLot = hoTenLot;
			entity.Ten = ten;
			entity.Lop = lop;
			entity.DiemTBCTK = diemTBCTK;
			entity.KyLuat = kyLuat;
			entity.MontTN_ChinhTri = montTN_ChinhTri;
			entity.MonTN_LyThuyet = monTN_LyThuyet;
			entity.MonTN_ThucHanh = monTN_ThucHanh;
			entity.DTB_TotNghiep = dTB_TotNghiep;
			entity.DXL_TotNghiep = dXL_TotNghiep;
			entity.TyLeDVHPTL = tyLeDVHPTL;
			entity.CongNhanTN = congNhanTN;
			entity.XepLoaiTN = xepLoaiTN;
			entity.GhiChu = ghiChu;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
			db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@DiemTBCTK", SqlDbType.Float, DiemTBCTK);
			db.AddInParameter(dbCommand, "@KyLuat", SqlDbType.DateTime, KyLuat.Year <= 1753 ? DBNull.Value : (object) KyLuat);
			db.AddInParameter(dbCommand, "@MontTN_ChinhTri", SqlDbType.Float, MontTN_ChinhTri);
			db.AddInParameter(dbCommand, "@MonTN_LyThuyet", SqlDbType.Float, MonTN_LyThuyet);
			db.AddInParameter(dbCommand, "@MonTN_ThucHanh", SqlDbType.Float, MonTN_ThucHanh);
			db.AddInParameter(dbCommand, "@DTB_TotNghiep", SqlDbType.Float, DTB_TotNghiep);
			db.AddInParameter(dbCommand, "@DXL_TotNghiep", SqlDbType.Float, DXL_TotNghiep);
			db.AddInParameter(dbCommand, "@TyLeDVHPTL", SqlDbType.Float, TyLeDVHPTL);
			db.AddInParameter(dbCommand, "@CongNhanTN", SqlDbType.NVarChar, CongNhanTN);
			db.AddInParameter(dbCommand, "@XepLoaiTN", SqlDbType.NVarChar, XepLoaiTN);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<TotNghiepTC> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepTC item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateTotNghiepTC(string maSV, string hoTenLot, string ten, string lop, double diemTBCTK, DateTime kyLuat, double montTN_ChinhTri, double monTN_LyThuyet, double monTN_ThucHanh, double dTB_TotNghiep, double dXL_TotNghiep, double tyLeDVHPTL, string congNhanTN, string xepLoaiTN, string ghiChu)
		{
			TotNghiepTC entity = new TotNghiepTC();			
			entity.MaSV = maSV;
			entity.HoTenLot = hoTenLot;
			entity.Ten = ten;
			entity.Lop = lop;
			entity.DiemTBCTK = diemTBCTK;
			entity.KyLuat = kyLuat;
			entity.MontTN_ChinhTri = montTN_ChinhTri;
			entity.MonTN_LyThuyet = monTN_LyThuyet;
			entity.MonTN_ThucHanh = monTN_ThucHanh;
			entity.DTB_TotNghiep = dTB_TotNghiep;
			entity.DXL_TotNghiep = dXL_TotNghiep;
			entity.TyLeDVHPTL = tyLeDVHPTL;
			entity.CongNhanTN = congNhanTN;
			entity.XepLoaiTN = xepLoaiTN;
			entity.GhiChu = ghiChu;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_CDTM_TCD_TotNghiepTC_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
			db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@DiemTBCTK", SqlDbType.Float, DiemTBCTK);
			db.AddInParameter(dbCommand, "@KyLuat", SqlDbType.DateTime, KyLuat.Year == 1753 ? DBNull.Value : (object) KyLuat);
			db.AddInParameter(dbCommand, "@MontTN_ChinhTri", SqlDbType.Float, MontTN_ChinhTri);
			db.AddInParameter(dbCommand, "@MonTN_LyThuyet", SqlDbType.Float, MonTN_LyThuyet);
			db.AddInParameter(dbCommand, "@MonTN_ThucHanh", SqlDbType.Float, MonTN_ThucHanh);
			db.AddInParameter(dbCommand, "@DTB_TotNghiep", SqlDbType.Float, DTB_TotNghiep);
			db.AddInParameter(dbCommand, "@DXL_TotNghiep", SqlDbType.Float, DXL_TotNghiep);
			db.AddInParameter(dbCommand, "@TyLeDVHPTL", SqlDbType.Float, TyLeDVHPTL);
			db.AddInParameter(dbCommand, "@CongNhanTN", SqlDbType.NVarChar, CongNhanTN);
			db.AddInParameter(dbCommand, "@XepLoaiTN", SqlDbType.NVarChar, XepLoaiTN);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<TotNghiepTC> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepTC item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateTotNghiepTC(string maSV, string hoTenLot, string ten, string lop, double diemTBCTK, DateTime kyLuat, double montTN_ChinhTri, double monTN_LyThuyet, double monTN_ThucHanh, double dTB_TotNghiep, double dXL_TotNghiep, double tyLeDVHPTL, string congNhanTN, string xepLoaiTN, string ghiChu)
		{
			TotNghiepTC entity = new TotNghiepTC();			
			entity.MaSV = maSV;
			entity.HoTenLot = hoTenLot;
			entity.Ten = ten;
			entity.Lop = lop;
			entity.DiemTBCTK = diemTBCTK;
			entity.KyLuat = kyLuat;
			entity.MontTN_ChinhTri = montTN_ChinhTri;
			entity.MonTN_LyThuyet = monTN_LyThuyet;
			entity.MonTN_ThucHanh = monTN_ThucHanh;
			entity.DTB_TotNghiep = dTB_TotNghiep;
			entity.DXL_TotNghiep = dXL_TotNghiep;
			entity.TyLeDVHPTL = tyLeDVHPTL;
			entity.CongNhanTN = congNhanTN;
			entity.XepLoaiTN = xepLoaiTN;
			entity.GhiChu = ghiChu;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
			db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@DiemTBCTK", SqlDbType.Float, DiemTBCTK);
			db.AddInParameter(dbCommand, "@KyLuat", SqlDbType.DateTime, KyLuat.Year == 1753 ? DBNull.Value : (object) KyLuat);
			db.AddInParameter(dbCommand, "@MontTN_ChinhTri", SqlDbType.Float, MontTN_ChinhTri);
			db.AddInParameter(dbCommand, "@MonTN_LyThuyet", SqlDbType.Float, MonTN_LyThuyet);
			db.AddInParameter(dbCommand, "@MonTN_ThucHanh", SqlDbType.Float, MonTN_ThucHanh);
			db.AddInParameter(dbCommand, "@DTB_TotNghiep", SqlDbType.Float, DTB_TotNghiep);
			db.AddInParameter(dbCommand, "@DXL_TotNghiep", SqlDbType.Float, DXL_TotNghiep);
			db.AddInParameter(dbCommand, "@TyLeDVHPTL", SqlDbType.Float, TyLeDVHPTL);
			db.AddInParameter(dbCommand, "@CongNhanTN", SqlDbType.NVarChar, CongNhanTN);
			db.AddInParameter(dbCommand, "@XepLoaiTN", SqlDbType.NVarChar, XepLoaiTN);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<TotNghiepTC> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepTC item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteTotNghiepTC(string maSV)
		{
			TotNghiepTC entity = new TotNghiepTC();
			entity.MaSV = maSV;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepTC_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<TotNghiepTC> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepTC item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}